package Listeners;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import Model.Paper;

/**
 * The listener for the download button of a Paper or Recommendation File.
 * (Not implemented. Only used for download_button, download)
 * @author Irving Estrera
 * */
@SuppressWarnings("serial")
public class DownloadListener extends AbstractAction
{
  private final Paper my_paper;
  private final int my_form_type;

  /**
   * A constructor for downloading either the Manuscript file or the
   * Recommendation file of a conference paper.
   * 
   * @param the_authors_paper
   * @param the_type_of_form
   *          0 for Manuscript. 1 for Recommendation form.
   * */
  public DownloadListener(final Paper the_authors_paper,
      final int the_type_of_form)
  {
    my_paper = the_authors_paper;
    my_form_type = the_type_of_form;
  }

  @Override
  public void actionPerformed(ActionEvent arg0)
  {
    if (my_paper.getManuscript() != null)
    {
      // save file in this line
//      System.out.println(my_paper.getRecommendation());
//      System.err.println(my_paper.getRecommendation() != null);
      final JFileChooser jfc = new JFileChooser(System.getProperty("user.home"));
      if (jfc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION)
      {
        try
        {
          switch (my_form_type)
          {
          case 0:
            saveFile(my_paper.getManuscript(), jfc.getSelectedFile()
                .getAbsolutePath());
            break;
          case 1:
            if (my_paper.getRecommendation() != null)
            {
              saveFile(my_paper.getRecommendation(), jfc.getSelectedFile()
                  .getAbsolutePath());
            } else
            {
              JOptionPane.showMessageDialog(null,
                  "There is no recommendation associated"
                      + " with this paper yet.");
            }
            break;
          default:
            throw new IllegalArgumentException(
                "Specified type of form to download invalid. " + "Type: "
                    + my_form_type);
          }
        } catch (final IOException the_exception)
        {
          the_exception.printStackTrace();
        }
      }
    } else
    {
      throw new IllegalArgumentException("Error saving file! File is null");
    }
  }

  /**
   * @author William Kim
   * */
  private static void saveFile(final File original,
      final String saved_file_location) throws IOException
  {
    FileInputStream in = null;
    FileOutputStream out = null;

    try
    {
      in = new FileInputStream(original);
      out = new FileOutputStream(saved_file_location);
      int c;

      while ((c = in.read()) != -1)
      {
        out.write(c);
      }
    } finally
    {
      if (in != null)
      {
        in.close();
      }
      if (out != null)
      {
        out.close();
      }
    }
  }
}
